<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActionController::PolymorphicRoutes</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActionController::PolymorphicRoutes</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../files/vendor/rails/actionpack/lib/action_controller/polymorphic_routes_rb.html">
                vendor/rails/actionpack/lib/action_controller/polymorphic_routes.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Polymorphic URL helpers are methods for smart resolution to a named route
call when given an Active Record model instance. They are to be used in
combination with <a href="Resources.html">ActionController::Resources</a>.
</p>
<p>
These methods are useful when you want to generate correct URL or path to a
RESTful resource without having to know the exact type of the record in
question.
</p>
<p>
Nested resources and/or namespaces are also supported, as illustrated in
the example:
</p>
<pre>
  polymorphic_url([:admin, @article, @comment])
</pre>
<p>
results in:
</p>
<pre>
  admin_article_comment_url(@article, @comment)
</pre>
<h2>Usage within the framework</h2>
<p>
Polymorphic URL helpers are used in a number of places throughout the <a
href="../Rails.html">Rails</a> framework:
</p>
<ul>
<li><tt>url_for</tt>, so you can use it with a record as the argument, e.g.
<tt>url_for(@article)</tt>;

</li>
<li><a
href="../ActionView/Helpers/FormHelper.html">ActionView::Helpers::FormHelper</a>
uses <tt><a
href="PolymorphicRoutes.html#M000898">polymorphic_path</a></tt>, so you can
write <tt>form_for(@article)</tt> without having to specify <tt>:url</tt>
parameter for the form action;

</li>
<li><tt>redirect_to</tt> (which, in fact, uses <tt>url_for</tt>) so you can
write <tt>redirect_to(post)</tt> in your controllers;

</li>
<li><a
href="../ActionView/Helpers/AtomFeedHelper.html">ActionView::Helpers::AtomFeedHelper</a>,
so you don&#8216;t have to explicitly specify URLs for feed entries.

</li>
</ul>
<h2>Prefixed polymorphic helpers</h2>
<p>
In addition to <tt><a
href="PolymorphicRoutes.html#M000897">polymorphic_url</a></tt> and <tt><a
href="PolymorphicRoutes.html#M000898">polymorphic_path</a></tt> methods, a
number of prefixed helpers are available as a shorthand to <tt>:action
=&gt; &quot;&#8230;&quot;</tt> in options. Those are:
</p>
<ul>
<li><tt>edit_polymorphic_url</tt>, <tt>edit_polymorphic_path</tt>

</li>
<li><tt>new_polymorphic_url</tt>, <tt>new_polymorphic_path</tt>

</li>
<li><tt>formatted_polymorphic_url</tt>, <tt>formatted_polymorphic_path</tt>

</li>
</ul>
<p>
Example usage:
</p>
<pre>
  edit_polymorphic_path(@post)              # =&gt; &quot;/posts/1/edit&quot;
  formatted_polymorphic_path([@post, :pdf]) # =&gt; &quot;/posts/1.pdf&quot;
</pre>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000898">polymorphic_path</a>&nbsp;&nbsp;
      <a href="#M000897">polymorphic_url</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000898" class="method-detail">
        <a name="M000898"></a>

        <div class="method-heading">
          <a href="#M000898" class="method-signature">
          <span class="method-name">polymorphic_path</span><span class="method-args">(record_or_hash_or_array, options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns the path component of a URL for the given record. It uses <tt><a
href="PolymorphicRoutes.html#M000897">polymorphic_url</a></tt> with
<tt>:routing_type =&gt; :path</tt>.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000898-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000898-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/polymorphic_routes.rb, line 117</span>
117:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">polymorphic_path</span>(<span class="ruby-identifier">record_or_hash_or_array</span>, <span class="ruby-identifier">options</span> = {})
118:       <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:routing_type</span>] = <span class="ruby-identifier">:path</span>
119:       <span class="ruby-identifier">polymorphic_url</span>(<span class="ruby-identifier">record_or_hash_or_array</span>, <span class="ruby-identifier">options</span>)
120:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000897" class="method-detail">
        <a name="M000897"></a>

        <div class="method-heading">
          <a href="#M000897" class="method-signature">
          <span class="method-name">polymorphic_url</span><span class="method-args">(record_or_hash_or_array, options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Constructs a call to a named RESTful route for the given record and returns
the resulting URL string. For example:
</p>
<pre>
  # calls post_url(post)
  polymorphic_url(post) # =&gt; &quot;http://example.com/posts/1&quot;
  polymorphic_url([blog, post]) # =&gt; &quot;http://example.com/blogs/1/posts/1&quot;
  polymorphic_url([:admin, blog, post]) # =&gt; &quot;http://example.com/admin/blogs/1/posts/1&quot;
  polymorphic_url([user, :blog, post]) # =&gt; &quot;http://example.com/users/1/blog/posts/1&quot;
</pre>
<h4>Options</h4>
<ul>
<li><tt>:action</tt> - Specifies the action prefix for the named route:
<tt>:new</tt>, <tt>:edit</tt>, or <tt>:formatted</tt>. Default is no
prefix.

</li>
<li><tt>:routing_type</tt> - Allowed values are <tt>:path</tt> or
<tt>:url</tt>. Default is <tt>:url</tt>.

</li>
</ul>
<h4>Examples</h4>
<pre>
  # an Article record
  polymorphic_url(record)  # same as article_url(record)

  # a Comment record
  polymorphic_url(record)  # same as comment_url(record)

  # it recognizes new records and maps to the collection
  record = Comment.new
  polymorphic_url(record)  # same as comments_url()
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000897-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000897-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/polymorphic_routes.rb, line 74</span>
 74:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">polymorphic_url</span>(<span class="ruby-identifier">record_or_hash_or_array</span>, <span class="ruby-identifier">options</span> = {})
 75:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">record_or_hash_or_array</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Array</span>)
 76:         <span class="ruby-identifier">record_or_hash_or_array</span> = <span class="ruby-identifier">record_or_hash_or_array</span>.<span class="ruby-identifier">compact</span>
 77:         <span class="ruby-identifier">record_or_hash_or_array</span> = <span class="ruby-identifier">record_or_hash_or_array</span>[<span class="ruby-value">0</span>] <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">record_or_hash_or_array</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">==</span> <span class="ruby-value">1</span>
 78:       <span class="ruby-keyword kw">end</span>
 79: 
 80:       <span class="ruby-identifier">record</span>    = <span class="ruby-identifier">extract_record</span>(<span class="ruby-identifier">record_or_hash_or_array</span>)
 81:       <span class="ruby-identifier">format</span>    = <span class="ruby-identifier">extract_format</span>(<span class="ruby-identifier">record_or_hash_or_array</span>, <span class="ruby-identifier">options</span>)
 82:       <span class="ruby-identifier">namespace</span> = <span class="ruby-identifier">extract_namespace</span>(<span class="ruby-identifier">record_or_hash_or_array</span>)
 83:       
 84:       <span class="ruby-identifier">args</span> = <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">record_or_hash_or_array</span>
 85:         <span class="ruby-keyword kw">when</span> <span class="ruby-constant">Hash</span>;  [ <span class="ruby-identifier">record_or_hash_or_array</span> ]
 86:         <span class="ruby-keyword kw">when</span> <span class="ruby-constant">Array</span>; <span class="ruby-identifier">record_or_hash_or_array</span>.<span class="ruby-identifier">dup</span>
 87:         <span class="ruby-keyword kw">else</span>        [ <span class="ruby-identifier">record_or_hash_or_array</span> ]
 88:       <span class="ruby-keyword kw">end</span>
 89: 
 90:       <span class="ruby-identifier">inflection</span> =
 91:         <span class="ruby-keyword kw">case</span>
 92:         <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:action</span>].<span class="ruby-identifier">to_s</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;new&quot;</span>
 93:           <span class="ruby-identifier">args</span>.<span class="ruby-identifier">pop</span>
 94:           <span class="ruby-identifier">:singular</span>
 95:         <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">record</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:new_record?</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">record</span>.<span class="ruby-identifier">new_record?</span>
 96:           <span class="ruby-identifier">args</span>.<span class="ruby-identifier">pop</span>
 97:           <span class="ruby-identifier">:plural</span>
 98:         <span class="ruby-keyword kw">else</span>
 99:           <span class="ruby-identifier">:singular</span>
100:         <span class="ruby-keyword kw">end</span>
101: 
102:       <span class="ruby-identifier">args</span>.<span class="ruby-identifier">delete_if</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">arg</span><span class="ruby-operator">|</span> <span class="ruby-identifier">arg</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Symbol</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">arg</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">String</span>)}
103:       <span class="ruby-identifier">args</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">format</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">format</span>
104:       
105:       <span class="ruby-identifier">named_route</span> = <span class="ruby-identifier">build_named_route_call</span>(<span class="ruby-identifier">record_or_hash_or_array</span>, <span class="ruby-identifier">namespace</span>, <span class="ruby-identifier">inflection</span>, <span class="ruby-identifier">options</span>)
106: 
107:       <span class="ruby-identifier">url_options</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">except</span>(<span class="ruby-identifier">:action</span>, <span class="ruby-identifier">:routing_type</span>, <span class="ruby-identifier">:format</span>)
108:       <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">url_options</span>.<span class="ruby-identifier">empty?</span>
109:         <span class="ruby-identifier">args</span>.<span class="ruby-identifier">last</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Hash</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">last</span>.<span class="ruby-identifier">merge!</span>(<span class="ruby-identifier">url_options</span>) <span class="ruby-operator">:</span> <span class="ruby-identifier">args</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">url_options</span>
110:       <span class="ruby-keyword kw">end</span>
111: 
112:       <span class="ruby-identifier">__send__</span>(<span class="ruby-identifier">named_route</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
113:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>